package com.zq.vaccinum.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zq.vaccinum.domain.entity.VaccinumType;
import com.zq.vaccinum.service.IVaccinumTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 疫苗类型 前端控制器
 * </p>
 *
 * @author zq
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/vaccinum-type")
public class VaccinumTypeController {

    @Autowired
    IVaccinumTypeService typeService;

    ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping("/queryName")
    public String queryName(String name) throws JsonProcessingException {
        HashMap hashMap = new HashMap();
        QueryWrapper<VaccinumType> queryWrapper = new QueryWrapper<>(new VaccinumType().setName(name));
        List<VaccinumType> list = typeService.list(queryWrapper);
        hashMap.put("list",list);
        return objectMapper.writeValueAsString(hashMap);
    }

    // 查询所有
    @RequestMapping("queryAll")
    public String list() throws JsonProcessingException {
        HashMap hashMap = new HashMap();
        List<VaccinumType> list = typeService.list();
        hashMap.put("list",list);
        return objectMapper.writeValueAsString(hashMap);
    }
    // 根据ID查一条数据
    @RequestMapping("queryOne")
    public String queryOne(int id) throws JsonProcessingException {
        VaccinumType byId = typeService.getById(id);
        return objectMapper.writeValueAsString(byId);
    }

    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(int id) throws JsonProcessingException {
        HashMap result = new HashMap();
        boolean b = typeService.removeById(id);
        result.put("ok", b);
        return objectMapper.writeValueAsString(result);
    }

    @RequestMapping("/update")
    public String update(VaccinumType vaccinumType) throws JsonProcessingException {
        HashMap result = new HashMap();
        boolean update = typeService.updateById(vaccinumType);
        result.put("ok", update);
        return objectMapper.writeValueAsString(result);
    }
    //疫苗类型新增
    @RequestMapping("/insert")
    public String insert(VaccinumType vaccinumType) throws JsonProcessingException {
        //map
        HashMap result = new HashMap();
        //使用业务层方法来执行添加操作
        boolean save = typeService.save(vaccinumType);
        //把结果添加到map中
        result.put("ok", save);
        //返回
        return objectMapper.writeValueAsString(result);
    }
    //查询接口，分页查询
    @RequestMapping("/list")
    public String listPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "4") Integer pageSize,
            @RequestParam(required = false, defaultValue = "")String keyword,
            @RequestParam(required = false, defaultValue = "")String name
            ) throws JsonProcessingException {
        //        map
        HashMap result = new HashMap();
        QueryWrapper queryWrapper = new QueryWrapper<VaccinumType>().like(keyword!=null&!keyword.equals(""),"name",keyword);
        queryWrapper.equals(name);
        //        查询list   分页查询使用page方法入参分页信息                          // 查询的页码  每页显示的条数
        Page<VaccinumType> page = typeService.page(new Page<VaccinumType>(pageNum, pageSize),queryWrapper);
        //        通过page来获取集合
        List<VaccinumType> list = page.getRecords();
        result.put("list", list);
        result.put("total", page.getTotal());
        //        解析、返回json
        return objectMapper.writeValueAsString(result);


    }
}
